<?php
namespace haibao\cms\business;
use \haibao\cms\data\PageControl as PageControlData;
use \haibao\cms\model\filter\PageControlCategory as PageControlCategoryFilter;
use \haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\PageControl as PageControlFilter;

class PageControl extends \haibao\cms\business\Base{
	
	public static function delControlData($id,$type){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_DEL_DATA);
		$filter = new \haibao\cms\model\filter\PageControlData();
		$filter->where(\haibao\cms\model\filter\PageControlData::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$data = new PageControlData();
		$result = $data->getAll($filter);
		$model = array_pop($result);
		if (!$model){
			throw new BusinessException('控件数据不存在');
		}
		if ($model->Status == \haibao\cms\model\data\PageControlData::STATUS_ON_LINE){
			throw new BusinessException('数据处于上线状态，无法删除');
		}
		
		$data->deleteDataById($id,$type);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_DEL_DATA,$id);
	}
	
	public static function addPageControlData($dataModel){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_ADD_DATA);
		if (!$dataModel->ParameterValue){
			throw new BusinessException('参数值不能为空');
		}
		
		$dataModel->CreateTime = new \DateTime();
		$dataModel->CreateBy = self::getCurrentUser()->Id;
		$dataModel->Status = \haibao\cms\model\data\PageControlData::STATUS_WAITING;
		
		$data = new PageControlData();
		
		$data->addPageControlData($dataModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_ADD_DATA, $dataModel->toArray());
	}
	
	public static function editPageControlData($dataModel){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_EDIT_DATA);
		if (!$dataModel->ParameterValue){
			throw new BusinessException('参数值不能为空');
		}

		$dataModel->CreateBy = self::getCurrentUser()->Id;
		$data = new PageControlData();

		$data->updatePageControlData($dataModel,self::getCurrentUser()->Id,new \DateTime());
		
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_EDIT_DATA);
	}
	
	public static function getPageControlById($id,$checkAddData = false){
		if (!$id){
			throw new BusinessException('控件id不能为空');
		}
		
		$filter = new \haibao\cms\model\filter\PageControl();
		$filter->where(\haibao\cms\model\filter\PageControl::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$filter->where(\haibao\cms\model\filter\PageControl::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,\haibao\cms\model\data\PageControl::STATUS_DISABLE);
		$data = new PageControlData();
		$result = $data->getAll($filter);
		if (!$result){
			throw new BusinessException('控件不存在');
		}
		$model = array_pop($result);
		
		if ($checkAddData){
			if (!$model->ManualData){
				throw new BusinessException('控件没有手动数据需要录入');
			}
			/* if ($model->RecordMaximum && self::getPageContolDataCount($id) >= $model->RecordMaximum){
				throw new BusinessException('控件数据只能录入'.$model->RecordMaximum.'条');
			} */
		}
		
		return $model;
	}
	
	public static function getPageContolDataCount($id){
		$data = new PageControlData();
		
		return $data->getPageContolDataCount($id);
	}
	
	public static function getAllPageControlData($filter){
		$data = new PageControlData();
		return $data->getAll($filter);
	}
	
	public static function getOnePageControlData($filter){
		$data = new PageControlData();
		return $data->getOne($filter);
	}
	
	/**
	 * 更新控件
	 * @param int $id
	 * @param array $arrSet
	 */
	public static function updatePageControl($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_EDIT);
		
		if (!$arrSet[PageControlFilter::CONDITION_FIELD_NAME_CONTROL_NAME]){
			throw new BusinessException('控件名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_NAME);
		}
		if (!$arrSet[PageControlFilter::CONDITION_FIELD_NAME_CONTROLLER]){
			throw new BusinessException('控制器名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_CONTROLLER);
		}
		if (!$arrSet[PageControlFilter::CONDITION_FIELD_NAME_CONTENT]){
			throw new BusinessException('内容不能为空',self::DATA_EMPTY_PAGE_CONTROL_CONTENT);
		}
		if (!$arrSet[PageControlFilter::CONDITION_FIELD_NAME_CATEGORY_ID]){
			throw new BusinessException('分类不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY);
		}
		
		$filter = new PageControlFilter();
		$filter->where(PageControlFilter::CONDITION_FIELD_NAME_CONTROL_NAME,Condition::CONDITION_EQUAL,$arrSet[PageControlFilter::CONDITION_FIELD_NAME_CONTROL_NAME]);
		$filter->where(PageControlFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
		if (self::getAllCategory($filter)){
			throw new BusinessException('控件名称已存在',self::DATA_ERROR_PAGE_CONTROL_NAME_EXIST);
		}
		
		$data = new PageControlData();
		
		$data->updateById($id, $arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_EDIT, array_merge($arrSet,array('id'=>$id)));
	}
	
	/**
	 * @param \haibao\cms\model\filter\PageControl $filterModel
	 */
	public static function getAllPageControl($filterModel){
		$data = new PageControlData();
		
		return $data->getAll($filterModel);
	}
	
	/**
	 * 添加控件
	 * @param \haibao\cms\model\data\PageControl $model
	 */
	public static function addPageControl($model){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_ADD);
		if (!$model->ControlName){
			throw new BusinessException('控件名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_NAME);
		}
		if (!$model->Controller){
			throw new BusinessException('控制器名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_CONTROLLER);
		}
		if (!$model->Content){
			throw new BusinessException('内容不能为空',self::DATA_EMPTY_PAGE_CONTROL_CONTENT);
		}
		if (!$model->CategoryId){
			throw new BusinessException('分类不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY);
		}
		
		$filter = new PageControlFilter();
		$filter->where(PageControlFilter::CONDITION_FIELD_NAME_CONTROL_NAME,Condition::CONDITION_EQUAL,$model->ControlName);
		if (self::getAllCategory($filter)){
			throw new BusinessException('控件名称已存在',self::DATA_ERROR_PAGE_CONTROL_NAME_EXIST);
		}
		
		$model->Status = \haibao\cms\model\data\PageControl::STATUS_ENABLE;
		$model->CreateTime = new \DateTime();
		$model->CreateBy = self::getCurrentUser()->Id;
		
		$data = new \haibao\cms\data\PageControl();
		$data->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_ADD, $model->toArray());
	}
	
	/**
	 * 更新控制器分类
	 * @param int $id
	 * @param array $arrSet
	 */
	public static function updatePageControlCategory($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT);
		if (!$id){
			throw new BusinessException('分类id不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY_ID);
		}
		if (!$arrSet[\haibao\cms\model\filter\PageControlCategory::CONDITION_FIELD_NAME_CATEGORY_NAME]){
			throw new BusinessException('分类名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY_NAME);
		}
		
		$filter = new \haibao\cms\model\filter\PageControlCategory();
		$filter->where(PageControlCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_NAME,Condition::CONDITION_EQUAL,$arrSet[PageControlCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_NAME]);
		$filter->where(PageControlCategoryFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
		if (self::getAllCategory($filter)){
			throw new BusinessException('分类名称已存在',self::DATA_ERROR_PAGE_CONTROL_CATEGORY_NAME_EXIST);
		}
		
		$data = new \haibao\cms\data\PageControl('\haibao\cms\model\data\PageControlCategory');
		$data->updateCategoryById($id, $arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT, array_merge($arrSet,array('id'=>$id)));
	}
	
	/**
	 * 添加控制器分类
	 * @param \haibao\cms\model\data\PageControlCategory $model
	 */
	public static function addPageControlCategory($model){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD);
		if (!$model->CategoryName){
			throw new BusinessException('分类名称不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY_NAME);
		}
		
		$filter = new \haibao\cms\model\filter\PageControlCategory();
		$filter->where(PageControlCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_NAME,Condition::CONDITION_EQUAL,$model->CategoryName);
		if (self::getAllCategory($filter)){
			throw new BusinessException('分类名称已存在',self::DATA_ERROR_PAGE_CONTROL_CATEGORY_NAME_EXIST);
		}
		
		$model->Status = \haibao\cms\model\data\PageControlCategory::STATUS_ENABLE;
		$data = new PageControlData('\haibao\cms\model\data\PageControlCategory');
		$data->addCategory($model);
	
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD, $model->toArray(true,true));
	}
	
	/**
	 * 修改控件状态
	 * @param int $id
	 * @param int $status
	 */
	public static function changControlStatus($id,$status){
		if (!$id){
			throw new BusinessException('控件分类id不能为空',self::DATA_EMPTY_PAGE_CONTROL_ID);
		}
		
		//TO DO 控件下还有实例
		
		$data = new \haibao\cms\data\PageControl();
		$data->updateById($id, array(\haibao\cms\model\filter\PageControl::CONDITION_FIELD_NAME_STATUS=>$status));
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_EDIT, array('id'=>$id,'status'=>$status));
	}
	
	/**
	 * 修改控件分类状态
	 * @param int $id
	 * @param int $status
	 * @throws BusinessException
	 */
	public static function changeCategoryStatus($id,$status){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT);
		if (!$id){
			throw new BusinessException('控件分类id不能为空',self::DATA_EMPTY_PAGE_CONTROL_CATEGORY_ID);
		}
		
		if ($status == \haibao\cms\model\data\PageControlCategory::STATUS_DISABLE){
			$filter = new PageControlFilter();
			$filter->where(PageControlFilter::CONDITION_FIELD_NAME_CATEGORY_ID,Condition::CONDITION_EQUAL,$id);
			if (self::getAllPageControl($filter)){
				throw new BusinessException('分类下还有控件，不能禁用',self::DATA_ERROR_PAGE_CONTROL_CATEGORY_CAN_NOT_DELETE);
			}
		}
		
		$data = new \haibao\cms\data\PageControl('\haibao\cms\model\data\PageControlCategory');
		$data->updateById($id, array(\haibao\cms\model\filter\PageControlCategory::CONDITION_FIELD_NAME_STATUS=>$status));
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT, array('id'=>$id,'status'=>$status));
	}
	
	/**
	 * 查询控件分类
	 * @param \haibao\cms\model\filter\PageControlCategory $filterModel
	 */
	public static function getAllCategory($filterModel){
		$data = new PageControlData();
		
		return $data->getAllCategory($filterModel);
	}

	public static function getPageControlDataById($id){
	    $data = new PageControlData();
	    return $data->getPageControlDataById($id);
	}
	
	public static function getPageControlDataByControlId($cid){
		$data = new PageControlData();
		return $data->getPageControlDataByControlId($cid);
	}
	
	public static function getOnLinePageControlDataByControlId($controlId){
	    $data = new PageControlData();
	    return $data->getOnLinePageControlDataByControlId($controlId);
	}
	
	/**
	 * 更新控件手动数据状态
	 * @param : $id
	 * @param : $status
	 */
	public static function changeControlDataStatus($id,$status = \haibao\cms\model\data\PageControlData::STATIS_ON_LINE){

		$data = new PageControlData();
		$data->changeControlDataStatus($id,$status);
		$data->updateById($id, array(
			\haibao\cms\model\filter\PageControlData::CONDITION_FIELD_NAME_STATUS=>$status	
		));
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_EDIT, array('id'=>$id,'status'=>$status));
	}
	
	/**
	 * 修改控件点击量
	 * @param int $id
	 */
	public static function setControlDataClickNum($id){
		$data = new PageControlData();
		$data->setControlDataClickNum($id);
	}
	
	/**
	 * 对控件的数据重排
	 * @param array $idArr
	 */
	public static function reSortOnlineData($idArr){
		$data = new PageControlData();
		$data->reSortOnlineData($idArr);
	}
}